import requests
import json


class TokenApi:
    def __init__(self):
        pass

    def token(self, username, password):
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36',
            'Accept': 'application/json, text/plain, */*',
            'sec-ch-ua': '"Chromium";v="106"',
            'accept-language': 'zh-CN,zh;q=1',
            'sec-ch-ua-mobile': '?0',
            'authorization': 'Basic Y2xpZW50Ok5ZSTRDRk9HUVZkOE5adWs=',
            'x-requested-with': 'XMLHttpRequest',
            'sec-ch-ua-platform': '"Windows"',
            'origin': 'http://local.shadowbot.com',
            'sec-fetch-site': 'cross-site',
            'sec-fetch-mode': 'cors',
            'sec-fetch-dest': 'empty',
            'referer': 'http://local.shadowbot.com/',
        }

        files = {
            'username': (None, username),
            'password': (None,
                         password),
            'grant_type': (None, 'password'),
            'scope': (None, 'all'),
            'login_type': (None, 'remind'),
            'crypt': (None, 'metal'),
            'clientCode': (None, 'rpa-win'),
            'clientVersion': (None, '5.32.29'),
            'windowsAccount': (None, 'LAPTOP-4NILR91S\\15054'),
            'machineId': (None, 'EB9C888F68F9EF52499C3593C3B8932AA8309BD7BFCBFBFF000A06A4'),
            'hostName': (None, 'LAPTOP-4NILR91S'),
            'machineConfig': (None,
                              '{"mac":"40-C7-3C-FD-55-93","osName":"Windows 11 Home China(Microsoft Windows 10.0.26200)","osVersion":"10.0.26200.0","product":{"Caption":"计算机系统产品","Description":"计算机系统产品","IdentifyingNumber":"4BBBB25419800353","Name":"MASH-XX","Vendor":"WIKO","Version":"M1020","UUID":"20250422-40C7-3CFD-5593-40C73CFD5597"},"osType":"64位操作系统","cpu":{"cpuid":"BFEBFBFF000A06A4","manufacturer":"GenuineIntel","version":"","name":"Intel(R) Core(TM) Ultra 5 125H"},"memory":{"physicalMemory":"32278M","virtualMemory":"134217727M","availablePhysicalMemory":"15932M","availableVirtualMemory":"132048951M"},"screenInfo":{"screenCount":1,"resolution":["2880 * 1920"]},"defaultBrowser":"msedge"}'),
        }

        response = requests.post('https://api.winrobot360.com/oauth/token', headers=headers, files=files)

        try:
            json_data = response.json()
            formatted_json = json.dumps(json_data, indent=4, ensure_ascii=False)
            # print("JSON Content:")
            # print(formatted_json)
            return json_data
        except json.JSONDecodeError:
            print("Response is not valid JSON")


if __name__ == '__main__':
    token_api = TokenApi()
    # user_name = '17336606653'
    user_name = '19076196325'
    password = 'pBuXAJx2WmzMu6FbrWsA3DhwrI/q+tIplYVmpEfyevzHxNgUZ6oMDpXNVTn/q1GMcN3s1AD31HcjHk4OWC/AChhprGLqLBLYxEmjRmaC3cxod+AuGnWuvMARODV9gFj5qkxxSiTfCtQsAmWLzk440hmWoHSkWaKd+U9XvStY5A8='
    token_info = token_api.token(user_name, password)
    access_token = token_info.get('access_token')
    print(access_token)
